<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
    <link rel="stylesheet" href="../element-ui/element.css">
    <style>
        .register {
            width: 100%;
            height: 100%;
            background-size: 100% 100%;
        }

        .demo-ruleForm {
            width: 400px;
            height: 400px;
        }

        .col_txt1 {
            text-align: left;
        }

        .header_fidx {
            position: relative;
        }

        .getCode-button {
            border-radius: .3125rem;
            text-align: center;
        }

        .verifyPhone-button {
            color: #999;
            border-radius: .3125rem;
            text-align: center;
            font-size: 1rem;
        }
    </style>
</head>
<body>
<script src="../../vue/vue.js"></script>
<script src="../element-ui/element.js"></script>
<script src="../../jquery/jquery-3.4.1.js"></script>
<div id="register">
    <template>
        <div class="register">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="手机号" prop="phone">
                    <el-row type="flex" class="row-bg header_fidx" :span="24">
                        <el-col :span="20" class="col_txt1">
                            <a class="grid-content">
                                <el-input v-model.trim="ruleForm.phone" type="text" placeholder="请输入手机号" id="phone"
                                          name="phone" clearable/>
                                </el-input>
                            </a>
                        </el-col>
                    </el-row>
                    <%--<el-input v-model="ruleForm.phone" auto-complete="off"></el-input>--%>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-row type="flex" class="row-bg header_fidx" :span="24">
                        <el-col :span="20" class="col_txt1">
                            <a class="grid-content">
                                <el-input type="password" v-model="ruleForm.password" clearable></el-input>
                            </a>
                        </el-col>
                    </el-row>
                </el-form-item>
                <el-form-item label="确认密码" prop="checkPass">
                    <el-row type="flex" class="row-bg header_fidx" :span="24">
                        <el-col :span="20" class="col_txt1">
                            <a class="grid-content">
                                <el-input type="password" v-model="ruleForm.checkPass" clearable></el-input>
                            </a>
                        </el-col>
                    </el-row>
                </el-form-item>
                <el-form-item label="验证码" prop="verify">
                    <el-row type="flex" class="row-bg header_fidx" :span="24">
                        <el-col :span="11" class="col_txt1">
                            <a class="grid-content">
                                <el-input v-model.trim="ruleForm.verify" type="text" placeholder="请输入验证码" name="verify"
                                          clearable/>
                            </a>
                        </el-col>
                        <el-col :span="7" class="col_txt1">
                            <a class="grid-content">
                                <el-button @click="getCode()" v-show="isgetCode" style="margin-left: 14.5px"
                                           class="getCode-button">点击获取
                                </el-button>
                                <div v-show="!isgetCode" @click="reacquire()" :class="{ active: isActive }"
                                     class="verifyPhone-button">
                                    <el-button v-show="isActive" style="margin-left: 14.5px">重新获取</el-button>
                                    <el-button v-show="!isActive" style="margin-left: 14.5px" disabled>{{count}}s</el-button>
                                </div>
                            </a>
                        </el-col>
                    </el-row>
                </el-form-item>
                <el-form-item>
                    <el-button type="success" @click="submitForm('ruleForm')" style="margin-left: 30px">注册</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </template>
</div>
</body>
<script>
    var Main = {
        name: 'register',
        data() {
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'))
                } else if (value !== this.ruleForm.password) {
                    callback(new Error('两次输入密码不一致!'))
                } else {
                    callback()
                }
            };
            return {
                activeName: 'second',
                isgetCode: true,
                isActive: false,
                count: 0,
                timer: null,
                TIME_COUNT: 60,//验证码倒计时60s
                ruleForm: {
                    phone: '',//手机号
                    verify: "", //验证码
                },
                rules: {
                    password: [
                        {required: true, message: '密码不能为空', trigger: 'blur'},
                        {min: 6, max: 16, message: '密码必须是6-16位', trigger: 'blur'},
                        {
                            pattern: /^(?![A-Z]+$)(?![a-z]+$)(?!\d+$)(?![\W_]+$)\S/,
                            message: '密码必须包含数字、字母和下划线任意两种',
                            trigger: 'blur'
                        }
                    ],
                    checkPass: [
                        {required: true, message: '确认密码不能为空', trigger: 'blur'},
                        {validator: validatePass2, trigger: 'blur'}
                    ],
                    phone: [
                        {required: true, message: '请输入手机号', trigger: 'blur'},
                        // 这个只能验证手机号
                        {
                            pattern: /^0{0,1}(13[0-9]|15[7-9]|153|156|17[1-9]|18[1-9])[0-9]{8}$/,
                            message: '请输入合法手机号',
                            trigger: 'blur'
                        }
                    ],
                    verify: [
                        {required: true, message: '验证码不能为空', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            submitForm(formName) {
                var that = this;
                console.log(that.yzm);
                this.$refs[formName].validate(valid => {
                    if (valid) {
                        $.ajax({
                            type: 'post',
                            url: '/forum/user/register',
                            dataType: 'json',
                            data: {
                                userPhone: that.ruleForm.phone,
                                userPassword: that.ruleForm.password,
                                other2: that.ruleForm.verify,
                            },
                            success: function (data) {
                                console.log(data);
                                if (data.state === '200') {
                                    alert("注册成功");
                                    this.activeName="first";
                                } else {
                                    alert(data.msg);
                                }
                            }
                        })
                    }
                });
            },

            resetForm(formName) {
                // 实现重置表单元素数据
                this.$refs[formName].resetFields()
            },
            //获取验证码
            getyzm() {
                var that = this;
                $.ajax({
                    type: 'post',
                    url: '/forum/user/yzm',
                    dataType: 'json',
                    data: {
                        userPhone: that.ruleForm.phone,
                    },
                    success: function (data) {
                        console.log(data);
                        if (data.state === '200') {
                            that.yzm = data.data
                        }
                    }
                })
            },
            //计时器
            getCode() {
                this.isgetCode = false;
                if (!this.timer) {
                    this.count = this.TIME_COUNT;
                    this.isActive = false;
                    this.timer = setInterval(() => {
                        if (this.count > 0 && this.count <= this.TIME_COUNT) {
                            this.count--;
                        } else {
                            this.isActive = true;
                            clearInterval(this.timer);
                            this.timer = null;
                        }
                    }, 1000);
                }
                this.getyzm();
            },

            //重新获取验证码
            reacquire() {
                this.getCode(this.ruleForm);
            }
        }
    };
    var Ctor = Vue.extend(Main);
    new Ctor().$mount('#register');
</script>